#!/bin/bash
echo "Build"

#TARGET
export TARGET=./out/server
#GNU
export CXX=g++
#CXXFLAGS
export CXXFLAGS="-lpthread -Iinc"


build_code()
{
    nowdate=$(date "+%Y%m%d")
    echo $nowdate
    if [ ! -e ./log/$nowdate/ ]; then
        mkdir ./log/$nowdate
    fi
    time=$(TZ=UTC-8 date "+%H%M")
    #echo $time
    filename=./log/$nowdate/build_
    filename+=$time
    filename+=.log
    #echo $filename
    make | tee $filename
}

case $1 in 
    "-c")
        echo "Clear>>>"
        rm ./log/* -rf
        rm ./obj/* -rf
        rm ./out/* -rf
        rm ./Run/* -rf
        ;;
    "-d")
        echo "Debug>>>"
        CXXFLAGS+=-DDEBUG
        build_code
        ;;
    "-test")
        echo "Test>>>"
        echo $CXX
        echo $TARGET
        echo $CXXFLAGS
        make print
        build_code
        ;;
    "-re")
        echo "reBuild project>>>"
        make clean
        build_code
        echo "**************************************************************************************************************************"
        if [ -e $TARGET ]; then
            chmod +x $TARGET
            $TARGET
        fi
        ;;
    *)
        echo "Default mode"
        if [ -e $TARGET ]; then
            rm $TARGET
        fi
        build_code
        echo "**************************************************************************************************************************"
        if [ -e $TARGET ]; then
            chmod +x $TARGET
            $TARGET
        fi
        ;;
esac
